package com.ray.common.util;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.util.Assert;

public class SheetRW
{
  private Sheet sheet;
  private Logger logger = Logger.getLogger(getClass());
  private Row currentRow;

  public SheetRW(Sheet sheet)
  {
    this.sheet = sheet;
  }

  public void setCurrentRow(int row) {
    this.currentRow = this.sheet.getRow(row);
    if (this.currentRow == null)
      this.currentRow = this.sheet.createRow(row);
  }

  public Object read(int col)
  {
    Cell cell = this.currentRow.getCell(col);
    return readCellVal(cell);
  }

  public Object read(int row, int col) {
    Assert.notNull(this.sheet, "sheet 不能为空!");
    Row hrow = this.sheet.getRow(row);
    if (hrow == null) {
      if (this.logger.isInfoEnabled()) {
        this.logger.info("找不到第" + row + "行");
      }
      return null;
    }
    Cell cell = hrow.getCell(col);
    if (cell == null) {
      if (this.logger.isInfoEnabled()) {
        this.logger.info("找不到第" + col + "列");
      }
      return null;
    }
    return readCellVal(cell);
  }

  private Object readCellVal(Cell cell) {
    Object o = null;
    if (cell == null) {
      return null;
    }
    switch (cell.getCellType())
    {
    case 4:
      o = Boolean.valueOf(cell.getBooleanCellValue());
      break;
    case 0:
      if (DateUtil.isCellDateFormatted(cell))
      {
        o = cell.getDateCellValue();
      }
      else {
        o = Double.valueOf(cell.getNumericCellValue());
      }
      break;
    case 2:
      o = cell.getCellFormula();
      break;
    case 1:
      o = cell.getRichStringCellValue().toString();
    case 3:
    }
    return o;
  }

  public Object read(int row, String col) {
    return read(row, ColumnHelp.change(col));
  }

  public void write(int row, int col, Object data) {
    Assert.notNull(this.sheet, "sheet 不能为空!");

    Row hssfRow = this.sheet.getRow(row);
    if (hssfRow == null) {
      hssfRow = this.sheet.createRow(row);
    }
    Cell cell = hssfRow.getCell(col);
    if (cell == null) {
      cell = hssfRow.createCell(col);
    }

    writeCellVal(data, cell);
    if (this.logger.isDebugEnabled())
      this.logger.debug(row + "行," + col + "列插入数据:" + data);
  }

  public void write(int col, Object data)
  {
    Cell cell = this.currentRow.getCell(col);
    if (cell == null) {
      cell = this.currentRow.createCell(col);
    }
    writeCellVal(data, cell);
  }

  private void writeCellVal(Object data, Cell cell) {
    if (data != null)
      if ((data instanceof String)) {
        cell.setCellValue((String)data);
      } else if ((data instanceof BigDecimal)) {
        BigDecimal bigDecimal = (BigDecimal)data;
        cell.setCellValue(bigDecimal.doubleValue());
      } else if ((data instanceof Calendar)) {
        cell.setCellValue((Calendar)data);
      } else if ((data instanceof java.sql.Date)) {
        String d = changeDate((java.sql.Date)data);
        cell.setCellValue(d);
      } else if ((data instanceof Timestamp)) {
        String d = changeDate((Timestamp)data);
        cell.setCellValue(d);
      } else if ((data instanceof Boolean)) {
        cell.setCellValue(((Boolean)data).booleanValue());
      } else {
        cell.setCellValue(data.toString());
      }
  }

  public void write(int row, String col, Object data)
  {
    int c = ColumnHelp.change(col);
    write(row, c, data);
  }

  private String changeDate(java.util.Date date) {
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    return f.format(date);
  }

  public void setSheet(HSSFSheet sheet) {
    this.sheet = sheet;
  }
}
